﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Drawing;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.Script.Serialization;

namespace TienTeTuong.back_end
{
    public partial class ManageController : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                String action = Request.Params["action"] ?? "";
                ProductDetailBUS pdBus = new ProductDetailBUS();
                CategoryDetailBUS ctdBus = new CategoryDetailBUS();
                CategoryBUS ctBus = new CategoryBUS();
                SponsorDAO spDao = new SponsorDAO();
                Order order = new Order();
                String result = "false";
                if (action.Equals("DeletePD"))
                {
                    String sID = Request.Params["id"] ?? "";
                    if (pdBus.deleteProduct(sID))
                    {
                        result = "true";
                    }
                }
                else if (action.Equals("DeleteCD"))
                {
                    String sID = Request.Params["id"] ?? "";
                    if (ctdBus.delete(sID))
                    {
                        result = "true";
                    }
                }
                else if (action.Equals("COS"))
                {
                    String sID = Request.Params["id"] ?? "";
                    String adminNote = Request.Params["note"] ?? "";
                    String Status = Request.Params["st"] ?? "";
                    Order or = new Order();
                    if (or.changeStatus(adminNote, Status, sID))
                    {
                        result = "true";
                    }
                }
                else if (action.Equals("DeleteCT"))
                {
                    String sID = Request.Params["id"] ?? "";
                    if (ctBus.delete(sID))
                    {
                        result = "true";
                    }
                }
                else if (action.Equals("DeleteOd"))
                {
                    String sID = Request.Params["id"] ?? "";
                    if (order.delete(sID))
                    {
                        result = "true";
                    }
                }
                else if (action.Equals("DeleteOds"))
                {
                    String sID = Request.Params["ids"] ?? "";
                    List<string> ids = sID.Split('/').ToList();
                    bool flag = true;
                    foreach ( var item in ids)
                    {
                        if(!string.IsNullOrEmpty(item)){
                            flag = flag && order.delete(item);
                        }
                    }

                    result = flag.ToString();
                }
                else if (action.Equals("DeleteSP"))
                {
                    String ID = Request.Params["id"] ?? "";
                    if (!spDao.delete(ID))
                    {
                        result = "true";
                    }
                }
                else if (action.Equals("SavePD"))
                {
                    String sID = Request.Params["id"] ?? "";
                    var item = new ProductDetailDTO();
                    item.Name = Request.Params["name"] ?? "";
                    item.Description = Request.Params["description"] ?? "";
                    item.Price = Double.Parse(Request.Params["price"]);
                    if (!String.IsNullOrEmpty(Request.Params["quantity"]))
                    {
                        item.Quantity = Double.Parse(Request.Params["quantity"]);
                    }
                    item.Unit = Request.Params["unit"] ?? "";
                    item.ProID = sID;
                    if (pdBus.saveProduct(item))
                    {
                        result = "true";
                    }
                }
                else if (action.Equals("AddPD"))
                {
                    var item = new ProductDetailDTO();
                    item.Name = Request.Params["name"] ?? "";
                    item.Description = Request.Params["description"] ?? "";
                    item.Price = Double.Parse(Request.Params["price"]);
                    if (!String.IsNullOrEmpty(Request.Params["quantity"]))
                    {
                        item.Quantity = Double.Parse(Request.Params["quantity"]);
                    }
                    item.Unit = Request.Params["unit"] ?? "";
                    if (pdBus.addProduct(item))
                    {
                        result = "true";
                    }
                }
                else if (action.Equals("imgUp"))
                {
                    try
                    {
                        HttpPostedFile eventPhoto = Request.Files["img"];
                        String sID = Request.Params["id"] ?? "";
                        if (eventPhoto.ContentLength > 0)
                        {
                            Guid guidName = Guid.NewGuid();
                            string fileName = string.Empty;
                            fileName = "_" + DateTime.Now.ToBinary() + "_" + Path.GetFileName(eventPhoto.FileName);

                            // Physical file path  for saving
                            string physicalFilePath = Path.Combine(HttpContext.Current.Request.ServerVariables["APPL_PHYSICAL_PATH"].ToString() + "//Images//upload", fileName);

                            // File path for display image
                            string filePath = "/Images/upload/" + fileName;

                            eventPhoto.SaveAs(physicalFilePath);
                            Session["img"] = filePath;
                            var serializer = new JavaScriptSerializer();
                            result = serializer.Serialize(new
                            {
                                aaData = filePath
                            });
                        }
                        else
                        {
                            result = "falseing";
                        }

                    }
                    catch
                    {
                        //return Json("error");
                        result = "false";
                    }
                }
                else if (action.Equals("imgUp1"))
                {
                    try
                    {
                        HttpPostedFile eventPhoto = Request.Files["img"];
                        String sID = Request.Params["id"] ?? "";
                        if (eventPhoto.ContentLength > 0)
                        {
                            Guid guidName = Guid.NewGuid();
                            string fileName = string.Empty;
                            fileName = "_" + DateTime.Now.ToBinary() + "_" + Path.GetFileName(eventPhoto.FileName);

                            // Physical file path  for saving
                            string physicalFilePath = Path.Combine(HttpContext.Current.Request.ServerVariables["APPL_PHYSICAL_PATH"].ToString() + "//Images//upload", fileName);

                            // File path for display image
                            string filePath = "/Images/upload/" + fileName;

                            eventPhoto.SaveAs(physicalFilePath);
                            Session["img1"] = filePath;
                            var serializer = new JavaScriptSerializer();
                            result = serializer.Serialize(new
                            {
                                aaData = filePath
                            });
                        }
                        else
                        {
                            result = "falseing";
                        }

                    }
                    catch
                    {
                        //return Json("error");
                        result = "false";
                    }
                }
                else if (action.Equals("imgUp2"))
                {
                    try
                    {
                        HttpPostedFile eventPhoto = Request.Files["img"];
                        String sID = Request.Params["id"] ?? "";
                        if (eventPhoto.ContentLength > 0)
                        {
                            Guid guidName = Guid.NewGuid();
                            string fileName = string.Empty;
                            fileName = "_" + DateTime.Now.ToBinary() + "_" + Path.GetFileName(eventPhoto.FileName);

                            // Physical file path  for saving
                            string physicalFilePath = Path.Combine(HttpContext.Current.Request.ServerVariables["APPL_PHYSICAL_PATH"].ToString() + "//Images//upload", fileName);

                            // File path for display image
                            string filePath = "/Images/upload/" + fileName;

                            eventPhoto.SaveAs(physicalFilePath);
                            Session["img2"] = filePath;
                            var serializer = new JavaScriptSerializer();
                            result = serializer.Serialize(new
                            {
                                aaData = filePath
                            });
                        }
                        else
                        {
                            result = "falseing";
                        }

                    }
                    catch
                    {
                        //return Json("error");
                        result = "false";
                    }
                }
                else if (action.Equals("imgArtUp"))
                {
                    try
                    {
                        HttpPostedFile eventPhoto = Request.Files["img"];
                        String sID = Request.Params["id"] ?? "";
                        if (eventPhoto.ContentLength > 0)
                        {
                            Guid guidName = Guid.NewGuid();
                            string fileName = string.Empty;
                            fileName = "_" + DateTime.Now.ToBinary() + "_" + Path.GetFileName(eventPhoto.FileName);

                            // Physical file path  for saving
                            string physicalFilePath = Path.Combine(HttpContext.Current.Request.ServerVariables["APPL_PHYSICAL_PATH"].ToString() + "//Images//upload", fileName);

                            // File path for display image
                            string filePath = "/Images/upload/" + fileName;

                            eventPhoto.SaveAs(physicalFilePath);
                            Session["imgArt"] = filePath;
                            var serializer = new JavaScriptSerializer();
                            result = serializer.Serialize(new
                            {
                                aaData = filePath
                            });
                        }
                        else
                        {
                            result = "falseing";
                        }

                    } 
                    catch
                    {
                        //return Json("error");
                        result = "false";
                    }
                }
                else if (action.Equals("imgArtUps"))
                {
                    try
                    {
                        HttpPostedFile eventPhoto = Request.Files["img"];
                        String sID = Request.Params["id"] ?? "";
                        if (eventPhoto.ContentLength > 0)
                        {
                            Guid guidName = Guid.NewGuid();
                            string fileName = string.Empty;
                            fileName = "_" + DateTime.Now.ToBinary() + "_" + Path.GetFileName(eventPhoto.FileName);

                            // Physical file path  for saving
                            string physicalFilePath = Path.Combine(HttpContext.Current.Request.ServerVariables["APPL_PHYSICAL_PATH"].ToString() + "//Images//upload", fileName);

                            // File path for display image
                            string filePath = "/Images/upload/" + fileName;

                            eventPhoto.SaveAs(physicalFilePath);
                            var serializer = new JavaScriptSerializer();
                            result = serializer.Serialize(new
                            {
                                aaData = filePath
                            });
                        }
                        else
                        {
                            result = "falseing";
                        }

                    }
                    catch
                    {
                        //return Json("error");
                        result = "false";
                    }
                }
                Response.Write(result);
            }
            catch
            {
                Response.Write("false");
            }
        }

        private System.Drawing.Image ScaleImage(System.Drawing.Image image, int maxWidth)
        {
            var ratio = (double)maxWidth / image.Width;

            if (ratio < 1)
            {
                var newWidth = (int)(image.Width * ratio);
                var newHeight = (int)(image.Height * ratio);

                var newImage = new Bitmap(newWidth, newHeight);
                Graphics.FromImage(newImage).DrawImage(image, 0, 0, newWidth, newHeight);
                return newImage;
            }
            return image;
        }
    }
}